using micromsg;
using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
using System.IO.IsolatedStorage;
namespace MicroMsg.Scene
{
	public class NetSceneDownloadAppAttach : NetSceneBaseEx<DownloadAppAttachRequest, DownloadAppAttachResponse, DownloadAppAttachRequest.Builder>
	{
		private const string TAG = "NetSceneDownloadAppAttach";
		public const int DOSCENE_SUCCESS = 1;
		public const int DOSCENE_SYS_ERR = 0;
		public const int DOSCENE_LOADING_ERR = -1;
		private int ONE_PACK_DOWNLOADSIZE = 65536;
		private AppMsgInfo mAppMsgInfo;
		private ChatMsg mChatMsg;
		private string mStrFilePath;
		private uint mStartPos;
		private bool isDoscene;
		public bool doScene(ChatMsg chatMsg_0)
		{
			if (this.isDoscene)
			{
				base.cancel();
				Log.smethod_1("NetSceneDownloadAppAttach", "id do sync now");
				return false;
			}
			if (chatMsg_0 == null || !chatMsg_0.IsAppMsg())
			{
				return false;
			}
			this.mChatMsg = chatMsg_0;
			this.mStartPos = 0u;
			this.mAppMsgInfo = AppMsgMgr.parseAppXml(this.mChatMsg.strMsg);
			if (this.mAppMsgInfo != null && !string.IsNullOrEmpty(this.mAppMsgInfo.attachid) && this.mAppMsgInfo.totallength > 0)
			{
				if (this.mAppMsgInfo.type == 2)
				{
					this.mStrFilePath = StorageIO.getAttachmentPath() + "/" + MD5Core.GetHashString(this.mAppMsgInfo.appid + "_" + this.mAppMsgInfo.attachid) + ".jpg";
				}
				else
				{
					if (this.mAppMsgInfo.type != 6)
					{
						Log.smethod_1("NetSceneDownloadAppAttach", "not support attach download");
						return false;
					}
					if (!AppMsgMgr.isFileSupport(this.mAppMsgInfo.title))
					{
						Log.smethod_5("NetSceneDownloadAppAttach", "not support file, filename =" + this.mAppMsgInfo.title);
						return false;
					}
					string text = this.mAppMsgInfo.title.Substring(this.mAppMsgInfo.title.IndexOf("."));
					this.mStrFilePath = StorageIO.getAttachmentPath() + "/" + MD5Core.GetHashString(this.mAppMsgInfo.appid + "_" + this.mAppMsgInfo.attachid) + text;
				}
				string progressString = this.getProgressString(0);
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_PROGRESS, new ParamDownloadAppAttach(progressString, this.mChatMsg.nMsgLocalID, this.mChatMsg.strTalker, RetConst.MM_OK, null), null);
				try
				{
					using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
					{
						if (userStoreForApplication.FileExists(this.mStrFilePath))
						{
							using (IsolatedStorageFileStream isolatedStorageFileStream = userStoreForApplication.OpenFile(this.mStrFilePath, 3))
							{
								this.mStartPos = (uint)isolatedStorageFileStream.get_Length();
							}
						}
					}
				}
				catch (Exception ex)
				{
					Log.smethod_1("NetSceneDownloadAppAttach", string.Concat(new object[]
					{
						"get File size Exception=",
						this.mStrFilePath,
						" \n",
						ex
					}));
				}
				if (this.isDownloadCompleted())
				{
					StorageIO.deleteFile(this.mStrFilePath);
					this.mStartPos = 0u;
				}
				this.doScene();
				return true;
			}
			return false;
		}
		private void doScene()
		{
			this.isDoscene = true;
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.AppId = this.mAppMsgInfo.appid;
			try
			{
				this.mBuilder.SdkVersion = uint.Parse(this.mAppMsgInfo.sdkVer);
			}
			catch (Exception)
			{
				this.mBuilder.SdkVersion = 0u;
			}
			this.mBuilder.MediaId = this.mAppMsgInfo.attachid;
			this.mBuilder.UserName = this.mAppMsgInfo.fromUserName;
			this.mBuilder.TotalLen = (uint)this.mAppMsgInfo.totallength;
			this.mBuilder.StartPos = this.mStartPos;
			this.mBuilder.DataLen = (uint)this.ONE_PACK_DOWNLOADSIZE;
			this.mSessionPack.mConnectMode = 2;
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/downloadappattach";
			this.mSessionPack.mHttpRecvProgress += new OnHttpReceivedProgressDelegate(this.OnHttpReceivedProgress);
			base.endBuilder();
		}
		private void OnHttpReceivedProgress(int length)
		{
			int byteLen = (int)(this.mStartPos + (uint)length);
			string progressString = this.getProgressString(byteLen);
			EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_PROGRESS, new ParamDownloadAppAttach(progressString, this.mChatMsg.nMsgLocalID, this.mChatMsg.strTalker, RetConst.MM_OK, null), null);
		}
		protected override void onFailed(DownloadAppAttachRequest request, DownloadAppAttachResponse response)
		{
			Log.smethod_1("NetSceneDownloadAppAttach", "system error DownLoad image failed !");
			this.isDoscene = false;
			EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_FAIL, new ParamDownloadAppAttach(null, this.mChatMsg.nMsgLocalID, this.mChatMsg.strTalker, RetConst.MM_ERR_CLIENT, null), null);
		}
		protected override void onSuccess(DownloadAppAttachRequest request, DownloadAppAttachResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				this.isDoscene = false;
				Log.smethod_1("NetSceneDownloadAppAttach", "Download failed because of net error");
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_FAIL, new ParamDownloadAppAttach(null, this.mChatMsg.nMsgLocalID, this.mChatMsg.strTalker, ret, null), null);
				return;
			}
			if (response.DataLen <= 0u)
			{
				this.isDoscene = false;
				Log.smethod_1("NetSceneDownloadAppAttach", "Download failed because of response.DataLen <= 0");
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_FAIL, new ParamDownloadAppAttach(null, this.mChatMsg.nMsgLocalID, this.mChatMsg.strTalker, ret, null), null);
				return;
			}
			if (response.Data != null)
			{
				if (response.DataLen == response.Data.ILen)
				{
					uint arg_C3_0 = response.StartPos;
					if (response.StartPos + response.DataLen > response.TotalLen)
					{
						this.isDoscene = false;
						Log.smethod_1("NetSceneDownloadAppAttach", "Download failed because of server image length error!");
						EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_FAIL, new ParamDownloadAppAttach(null, this.mChatMsg.nMsgLocalID, this.mChatMsg.strTalker, ret, null), null);
						return;
					}
					if (response.StartPos != this.mStartPos)
					{
						this.isDoscene = false;
						Log.smethod_1("NetSceneDownloadAppAttach", "Download failed because of position error!");
						EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_FAIL, new ParamDownloadAppAttach(null, this.mChatMsg.nMsgLocalID, this.mChatMsg.strTalker, ret, null), null);
						return;
					}
					if (response.TotalLen <= 0u)
					{
						this.isDoscene = false;
						Log.smethod_1("NetSceneDownloadAppAttach", "Download failed because of TotalLen error!");
						EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_FAIL, new ParamDownloadAppAttach(null, this.mChatMsg.nMsgLocalID, this.mChatMsg.strTalker, ret, null), null);
						return;
					}
					StorageIO.appendToFile(this.mStrFilePath, response.Data.Buffer.ToByteArray());
					this.mStartPos += response.DataLen;
					if (this.isDownloadCompleted())
					{
						this.isDoscene = false;
						Log.smethod_4("NetSceneDownloadAppAttach", "Download Complete!");
						this.mChatMsg.strPath = this.mStrFilePath;
						StorageMgr.chatMsg.modifyMsg(this.mChatMsg);
						EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_COMPLETE, new ParamDownloadAppAttach(null, this.mChatMsg.nMsgLocalID, this.mChatMsg.strTalker, ret, this.mStrFilePath), null);
						return;
					}
					this.doScene();
					return;
				}
			}
			this.isDoscene = false;
			Log.smethod_1("NetSceneDownloadAppAttach", "Download failed because of server param error!");
			EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_FAIL, new ParamDownloadAppAttach(null, this.mChatMsg.nMsgLocalID, this.mChatMsg.strTalker, ret, null), null);
		}
		public string getProgressString(int byteLen)
		{
			string text = ((double)byteLen * 1.0 / 1024.0).ToString();
			int num = text.IndexOf(".", 0);
			if (num == -1)
			{
				return text;
			}
			return text.Substring(0, num + 2);
		}
		private bool isDownloadCompleted()
		{
			return (ulong)this.mStartPos >= (ulong)((long)this.mAppMsgInfo.totallength) && this.mAppMsgInfo.totallength != 0;
		}
	}
}
